package com.sxc.loglib.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sxc.loglib.FileUtils;
import com.sxc.loglib.LogConfig;
import com.sxc.loglib.R;

import java.io.File;
import java.util.Arrays;

/**
 * Created by sunxunchao on 2020/1/6
 */
public class LogActivity extends Activity {

    public static void startLogActivity(Context context) {
        Intent intent = new Intent(context, LogActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private ListView ls;
    private TextView content;

    private LogAdapter adapter;

    private File currentFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_log);

        ls = findViewById(R.id.ls);
        content = findViewById(R.id.content);
        ls.setAdapter(adapter = new LogAdapter(LogActivity.this, R.layout.adapter_log_item));
        File[] files = FileUtils.getLogFileList();
        if (files != null) {
            adapter.addItems(Arrays.asList(files));
        }
        if (files != null && files.length > 0) {
            currentFile = files[0];
            content.setText(FileUtils.readLogText(currentFile));
            adapter.setSelectedItem(0);
        }

        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ls.getVisibility() == View.VISIBLE) {
                    ls.setVisibility(View.GONE);
                } else {
                    ls.setVisibility(View.VISIBLE);
                }
                continuousClick(COUNTS, DURATION);
            }
        });

        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentFile = (File) parent.getAdapter().getItem(position);
                content.setText(FileUtils.readLogText(currentFile));
                adapter.setSelectedItem(position);
            }
        });

        ls.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                File file = (File) parent.getAdapter().getItem(position);
                open(file);
                return false;
            }
        });

    }


    private final static int COUNTS = 5;// 点击次数
    private final static long DURATION = 5000;// 规定有效时间
    private long[] mHits = new long[COUNTS];
    private int times = 0;

    private void continuousClick(int count, long time) {
        times++;
        //每次点击时，数组向前移动一位
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        //为数组最后一位赋值
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
            showLong(this.getApplicationContext(), "点击了" + times + "次");
            mHits = new long[COUNTS];//重新初始化数组
            sendEmail(currentFile);
            times = 0;
        } else {
            showLong(this.getApplicationContext(), "点击了" + times + "次");
        }
    }

    private static Toast mToast;

    public static void showLong(Context context, String msg) {
        if (mToast == null)
            mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        else
            mToast.setText(msg);
        mToast.show();
    }

    private void open(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file), "text/plain");
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(LogActivity.this, "未找到能打开该文件的程序", Toast.LENGTH_LONG).show();
        }
    }

    private void sendEmail(File file) {
        if (file == null) return;
        String[] email = {"sunxunchao@gcsoft.com"}; // 需要注意，email必须以数组形式传入
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("application/octet-stream"); // 设置邮件格式
//        intent.setType("message/rfc882");
        intent.putExtra(Intent.EXTRA_EMAIL, email); // 接收人
//        intent.putExtra(Intent.EXTRA_CC, email); // 抄送人
        intent.putExtra(Intent.EXTRA_SUBJECT, file.getName()); // 主题
        intent.putExtra(Intent.EXTRA_TEXT, FileUtils.readLogText(file)); // 正文
        if (LogConfig.getInstance().getSaveExternal()) {
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        }
        startActivity(Intent.createChooser(intent, "请选择邮件类应用"));
    }
}
